package br.ufrj.dcc.dm.model.decisionTree;

import br.ufrj.dcc.dm.model.Attribute;
import br.ufrj.dcc.dm.model.Entry;

public class Tree 
{
	private Node rootNode;

	public String decide(Entry entry)
	{
		Node node = decide(rootNode, entry);
		
		if(node instanceof ClassNode)
		{
			ClassNode classNode = (ClassNode) node;
			return classNode.getDecisionClassValue();
		}
		else
		{
			/**
			 * TODO: Create exception for this
			 */
			return "UNRECOGNIZED_CLASS";
		}
	}
	
	private Node decide(Node parentNode, Entry entry)
	{
		Node node = null;
		
		for(Attribute attribute : entry.getAttributes())
		{
			if(attribute.getAttributeIndex() == parentNode.getAttributeIndex())
			{
				node = parentNode.decide(attribute.getValue());

				if(node instanceof ClassNode)
				{
					return node;
				}
				else
				{
					node = decide(node, entry);
				}
				
			}
		}
		
		return node;
	}
	public Node getRootNode() 
	{
		return rootNode;
	}

	public void setRootNode(Node rootNode) 
	{
		this.rootNode = rootNode;
	}

}
